package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.fragments.DiscussAllFragment;
import com.sxm.yiwei.fragments.DiscussFailFragment;
import com.sxm.yiwei.fragments.DiscussFunFragment;
import com.sxm.yiwei.fragments.DiscussPicFragment;
import com.sxm.yiwei.fragments.DiscussbetweenFragment;

public class FWAllDiscussActivity extends BaseActivity implements View.OnClickListener{

    private TextView all;
    private TextView pic;
    private TextView fun;
    private TextView fail;
    private TextView between;
    private ImageView alldiscussback;
    private DiscussAllFragment discussAllFragment;
    private DiscussFunFragment discussFunFragment;
    private DiscussPicFragment discussPicFragment;
    private DiscussbetweenFragment discussbetweenFragment;
    private DiscussFailFragment discussFailFragment;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fwall_discuss);
        initAllDiscussView();
        all.setSelected(true);
        setViewFragment(0);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //super.onSaveInstanceState(outState);
    }

    private void setViewFragment(int i) {
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        hideFragment(ft);
        switch (i){
            case 0:
                if (discussAllFragment==null){
                    discussAllFragment = new DiscussAllFragment();
                    ft.add(R.id.alldiscuss_fragment,discussAllFragment);
                }else {
                    ft.show(discussAllFragment);
                }
                break;
            case 1:
                if (discussPicFragment==null){
                    discussPicFragment = new DiscussPicFragment();
                    ft.add(R.id.alldiscuss_fragment,discussPicFragment);
                }else {
                    ft.show(discussPicFragment);
                }
                break;
            case 2:
                if (discussFunFragment==null){
                    discussFunFragment = new DiscussFunFragment();
                    ft.add(R.id.alldiscuss_fragment,discussFunFragment);
                }else {
                    ft.show(discussFunFragment);
                }
                break;
            case 3:
                if (discussbetweenFragment==null){
                    discussbetweenFragment = new DiscussbetweenFragment();
                    ft.add(R.id.alldiscuss_fragment,discussbetweenFragment);
                }else {
                    ft.show(discussbetweenFragment);
                }
                break;
            case 4:
                if (discussFailFragment==null){
                    discussFailFragment = new DiscussFailFragment();
                    ft.add(R.id.alldiscuss_fragment,discussFailFragment);
                }else {
                    ft.show(discussFailFragment);
                }
                break;
        }
        ft.commit();
    }

    private void hideFragment(FragmentTransaction ft) {
        if (discussAllFragment!=null){
            ft.hide(discussAllFragment);
        }
        if (discussFunFragment!=null){
            ft.hide(discussFunFragment);
        }
        if (discussPicFragment!=null){
            ft.hide(discussPicFragment);
        }
        if (discussbetweenFragment!=null){
            ft.hide(discussbetweenFragment);
        }
        if (discussFailFragment!=null){
            ft.hide(discussFailFragment);
        }


    }

    private void initAllDiscussView() {
        alldiscussback = (ImageView) findViewById(R.id.fw_alldiscuss_back);
        all = (TextView) findViewById(R.id.alldiscus_all);
        pic = (TextView) findViewById(R.id.alldiscuss_pic);
        fun = (TextView) findViewById(R.id.alldiscuss_fun);
        fail = (TextView) findViewById(R.id.alldiscuss_fail);
        between = (TextView) findViewById(R.id.aadiscuss_between);
        backmain = (ImageView) findViewById(R.id.fwalldis_backmain);
        backmain.setOnClickListener(this);
        alldiscussback.setOnClickListener(this);
        all.setOnClickListener(this);
        pic.setOnClickListener(this);
        fun.setOnClickListener(this);
        fail.setOnClickListener(this);
        between.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.fwalldis_backmain:

                startActivity(new Intent(FWAllDiscussActivity.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
            case R.id.alldiscus_all:
                all.setSelected(true);
                pic.setSelected(false);
                fun.setSelected(false);
                fail.setSelected(false);
                between.setSelected(false);
                setViewFragment(0);
                break;
            case R.id.alldiscuss_pic:
                setViewFragment(1);
                all.setSelected(false);
                pic.setSelected(true);
                fun.setSelected(false);
                fail.setSelected(false);
                between.setSelected(false);
                break;
            case R.id.alldiscuss_fun:
                setViewFragment(2);
                all.setSelected(false);
                pic.setSelected(false);
                fun.setSelected(true);
                fail.setSelected(false);
                between.setSelected(false);
                break;
            case R.id.alldiscuss_fail:
                setViewFragment(3);
                all.setSelected(false);
                pic.setSelected(false);
                fun.setSelected(false);
                fail.setSelected(true);
                between.setSelected(false);
                break;
            case R.id.fw_alldiscuss_back:
                finish();
                break;
            case R.id.aadiscuss_between:
                setViewFragment(4);
                all.setSelected(false);
                pic.setSelected(false);
                fun.setSelected(false);
                fail.setSelected(false);
                between.setSelected(true);
                break;
        }
    }
}
